var bibao = 8;
var round = 0;
$(document)
		.ready(
				function(){
					for (var i = 0; i < 15; i++)
						$("table")
								.append(
										"<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>");
					var allPlace = new Array();
					for (var i = 0; i < 15; i++)
						allPlace[i] = new Array();
					// 执黑
					$("#bFirst").click(function(){
						$("#wFirst").unbind("click");
						$("#bFirst").unbind("click");
						// alert("您选择了先手执黑");
						$("td").click(function(){
							$(this).unbind("click");
							var b = $(this).index();
							var a = $(this).parent().index();
							var local = b * 15 + a;
							$(this).css("background-image", "url('css/tdblack.jpg')");
							if (evaluate(allPlace, 1, a, b) > 30000000){
								alert("游戏结束,您赢了!");
								$("td").unbind("click");
								return;
							}
							allPlace[a][b] = 1;
							var loc2;
							if (++round < 3)
								loc2 = noThink(allPlace, -1);
							else
								loc2 = aiThink(allPlace, -1, bibao);
							b = loc2 % 15;
							a = (loc2 - b) / 15;
							$("tr").eq(a).find("td").eq(b).unbind("click");
							$("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdwhite.jpg')");
							allPlace[a][b] = -1;
						});
					});

					// 执白
					$("#wFirst").click(function(){
						$("#wFirst").unbind("click");
						$("#bFirst").unbind("click");
						// alert("您选择了后手执白");
						allPlace[7][7] = -1;
						$("td").eq(112).css("background-image", "url('css/tdblack.jpg')")
						$("td").click(function(){
							$(this).unbind("click");
							var b = $(this).index();
							var a = $(this).parent().index();
							var local = b * 15 + a;
							$(this).css("background-image", "url('css/tdwhite.jpg')");
							if (evaluate(allPlace, 1, a, b) > 30000000){
								alert("游戏结束,您赢了!");
								$("td").unbind("click");
								return;
							}
							allPlace[a][b] = 1;
							var loc2;
							if (++round < 3)
								loc2 = noThink(allPlace, -1);
							else
								loc2 = aiThink(allPlace, -1, bibao);
							b = loc2 % 15;
							a = (loc2 - b) / 15;
							$("tr").eq(a).find("td").eq(b).unbind("click");
							$("tr").eq(a).find("td").eq(b).css("background-image", "url('css/tdblack.jpg')");
							allPlace[a][b] = -1;
						});
					});

				});
function noThink(arr,sign){
	var score = 0,local;
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (!check(arr, i, j) || arr[i][j] != undefined)
				continue;
			var temp = evaluate(arr, sign, i, j);
			if (score < temp){
				local = i * 15 + j;
				score = temp;
			}
		}
	return local;
}

// 全棋盘分析
function aiThink(arr,sign,deep){
	var score = 0,score2 = 0;
	var local = 0;
	var a,b,c,d;
	var forb = [];
	var maybe = [
		{loc : -1, sc : 0}];
	// var sign2 = sign * (-1);
	// var test = new Array();
	//
	// for (var i = 0; i < 15; i++)
	// test[i] = arr[i].concat();
	// 所有位置评分并排序
	for (var i = 0; i < 15; i++)
		for (var j = 0; j < 15; j++){
			if (!check(arr, i, j) || arr[i][j] != undefined)
				continue;
			score = evaluate(arr, sign, i, j);
			local = i * 15 + j;
			maybe.push(
				{loc : local, sc : score});
		}
	maybe.sort(function(a,b){
		return b.sc > a.sc;
	});
	if (maybe[0].sc > 30000000)
		if (sign == 1)
			return -111;
		else{
			if (deep == bibao){
				if (maybe[0].sc > 30000000){
					$("td").unbind("click");
					alert("游戏结束,你输了!");
				}
				return maybe[0].loc;
			}
			return 1111;
		}
	// alert("test");
	// 递归出口
	if (deep == 0)
		return 2222;
	// 递归调用函数模拟下一步,如果下一步不理想则放弃此位置
	var flag = false;
	var limit = 16 < maybe.length ? 24 : maybe.length;
	for (var i = 0; i < limit; i++){
		b = maybe[i].loc % 15;
		a = (maybe[i].loc - b) / 15;
		arr[a][b] = sign;
		// score = maybe[i].sc;
		var loc2 = aiThink(arr, 0 - sign, deep - 1);
		arr[a][b] = undefined;
		if (loc2 == -111){
			continue;
		} else if (loc2 == 1111){
			continue;
		} else if (loc2 == 2222){
			return -111;
		} else if (loc2 == -222){
			return a * 15 + b;
		} else
			continue;
	}
	// if(sign=-1){
	if (deep == bibao){
		alert("meibanfa!");
		return maybe[0].loc;
	} else if (sign == -1)
		return 2222;
	else{
		return -222;
	}

}
// 位置检查
function check(arr,a,b){
	for (var i = a - 4 > 0 ? a - 4 : 0; i < a + 4 && i < 15; i++)
		for (var j = b - 4 > 0 ? b - 4 : 0; j < b + 4 && j < 15; j++)
			if (arr != undefined)
				return true;
	return false;
}
// 位置打分
function evaluate(arr,sign,a,b){
	var anti = 0 - sign;
	var score = 15 - Math.abs(a - 7) - Math.abs(b - 7);
	var troop = new Array();
	for (var i = 0; i < 4; i++)
		troop[i] = "";
	// 横
	for (var i = b - 4; i < b + 5; i++){
		if (i >= 0 && i < 15)
			if (i == b)
				troop[0] += '1';
			else if (arr[a][i] == sign)
				troop[0] += '1';
			else if (arr[a][i] == anti)
				troop[0] += '2';
			else
				troop[0] += '0';

	}

	// 竖
	for (var i = a - 4; i < a + 5; i++){
		if (i >= 0 && i < 15)
			if (i == a)
				troop[1] += '1';
			else if (arr[i][b] == sign)
				troop[1] += '1';
			else if (arr[i][b] == anti)
				troop[1] += '2';
			else
				troop[1] += '0';

	}

	// 捺\
	var i,j;
	for (i = a - 4, j = b - 4; i < a + 5 && j < b + 5; i++, j++){
		if (i >= 0 && i < 15 && j >= 0 && j < 15)
			if (i == a && j == b)
				troop[2] += '1';
			else if (arr[i][j] == sign)
				troop[2] += '1';
			else if (arr[i][j] == anti)
				troop[2] += '2';
			else
				troop[2] += '0';
	}

	// 撇/
	for (i = a - 4, j = b + 4; i < a + 5 && j > b - 5; i++, j--){
		if (i >= 0 && i < 15 && j >= 0 && j < 15)
			if (i == a && j == b)
				troop[3] += '1';
			else if (arr[i][j] == sign)
				troop[3] += '1';
			else if (arr[i][j] == anti)
				troop[3] += '2';
			else
				troop[3] += '0';
	}

	// 计算评分
	for (var i = 0; i <8; i++){
		if (/(11111)/g.test(troop[i]))
			score += 30000000;
		else if (/(12222)|(21222)|(22122)|(22212)|(22221)/g.test(troop[i]))
			score += 10000000;
		else if (/(011110)|(1011101)/g.test(troop[i]))
			score += 3000000;
		else if (/(11110)|(10111)|(01111)|(11101)|(11011)/g.test(troop[i]))
			score += 1500000;
		else if (/(012220)|(021220)|(022120)|(022210)|(2012202)|(2021202)|(2022102)/g.test(troop[i]))
			score += 800000;
		else if (/(011100)|(001110)|(010110)|(011010)/g.test(troop[i]))
			score += 200000;
		else if (/(012200)|(021200)|(022100)|(001220)|(002210)|(002120)|(020210)|(022010)/g.test(troop[i]))
			score += 40000;
		else if (/(001100)|(011000)|(000110)|(010100)/g.test(troop[i]))
			score += 5000;
		else if (/(11100)|(10110)|(11010)|(00111)|(01011)|(01101)|(11001)|(10101)|(10011)|(01110)(001010)|(010010)/g
				.test(troop[i]))
			score += 3500;
	}
	return score;
}
